package com.charles.video.system.controller;

import com.charles.video.common.core.responseDomain.AjaxResult;
import com.charles.video.common.core.responseDomain.TableInfo;
import com.charles.video.system.mapper.VideoCommentsMapper;
import com.charles.video.system.mapper.VideoCommentsReplyMapper;
import com.charles.video.system.mapper.VideoInfoMapper;
import com.charles.video.system.service.CommentsService;
import com.charles.video.system.vo.SearchCommentForm;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Charles-H
 * <p>
 * 评论控制层
 */
@RestController
@RequestMapping("/video/comment")
public class CommentsController extends BannerController {

    @Resource
    private CommentsService commentsService;

    @Resource
    private VideoInfoMapper videoInfoMapper;

    @Resource
    private VideoCommentsMapper videoCommentsMapper;

    @Resource
    private VideoCommentsReplyMapper videoCommentsReplyMapper;

    @GetMapping("/getCommentsData")
    public TableInfo getCommentsData(Integer pageNum, Integer pageSize, SearchCommentForm searchCommentForm) {
        return commentsService.queryCommentsData(pageNum, pageSize, searchCommentForm);
    }

    @DeleteMapping("/deleteComment/{commentId}")
    public AjaxResult deleteComment(@PathVariable("commentId") Long commentId, boolean isReply) {
        if (isReply) {
            videoInfoMapper.decreaseCommentCount(videoCommentsMapper.queryVideoId(videoCommentsReplyMapper.queryParentId(commentId)));
            return toAjax(videoCommentsReplyMapper.deleteComment(commentId));
        } else {
            videoInfoMapper.decreaseCommentCount(videoCommentsMapper.queryVideoId(commentId));
            return toAjax(videoCommentsMapper.deleteComment(commentId));
        }
    }

}
